/**
  ******************************************************************************
  * @author     Chris
  * @since      2023/9/29 22:38
  *
  * @file       hd_count_sensor.c
  * @brief      Count Sensor Hardware Driver.
  *
  * @note       This file contains the hardware driver for the Count Sensor.
  *
  * @warning    None.
  ******************************************************************************
  * Change Logs:
  *   Date           Author       Notes
  *   2023-09-29     Chris        the first version
  *
  ******************************************************************************
  */

#include "stm32g4xx_ll_bus.h"
#include "stm32g4xx_ll_exti.h"
#include "stm32g4xx_ll_system.h"
#include "pd_h_gpio.h"
#include "stm32g4xx_ll_gpio.h"
#include "delay.h"

/** region declare function region */
uint16_t CountSensor_Count;                //全局变量，用于计数
/** region declare function region */


/** region public function region */

/**
  * @brief  计次传感器初始化
  *
  * @note   计次传感器为各种通过 LM393 芯片数字输出（DO）引脚的传感器。
  * @param  None
  * @retval 无
  */
void CountSensor_Init(void) {
    PD_H_GPIO_CountSensorInit();

    // 中断线选择
    LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTA, LL_SYSCFG_EXTI_LINE2);

    // EXTI 配置
    LL_EXTI_InitTypeDef EXTI_InitStruct;
    EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_2;
    EXTI_InitStruct.LineCommand = ENABLE;
    EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
    EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;
    LL_EXTI_Init(&EXTI_InitStruct);

    // NVIC 配置优先级
    NVIC_SetPriority(EXTI2_IRQn,
                     NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 1, 1));

    // 使能 EXTI
    NVIC_EnableIRQ(EXTI2_IRQn);
}

/**
  * @brief  获取计数传感器的计数值
  * @param  None
  * @retval 计数值，范围：0~65535
  */
uint16_t CountSensor_Get(void) {
    return CountSensor_Count;
}


/** region public function region */


/** region private function region */

/** region private function region */
